package com.zyf.project.capchain.aop;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author Zhou Yifan
 */
@Aspect
@Component
@Slf4j
public class ServiceLogAspect {

    @Pointcut("execution(public * com.zyf.project.capchain.service.*.*(..))")
    public void serviceLog() {
        // TODO document why this method is empty
    }

    @Before("serviceLog()")
    public void doBefore(@NotNull JoinPoint point) {
        String methodName = point.getSignature().getName();
        List<Object> args = Arrays.asList(point.getArgs());
        log.info("METHOD : " + methodName);
        log.info("ARGS : " + StringUtils.join(args, ','));
    }

    @AfterReturning(returning = "ret", pointcut = "serviceLog()")
    public void doAfter(Object ret) {
        log.info("RETURN DATA : " + ret);
    }
}
